<template>
<div >
  <div>
    <JButton size="large" @click="startSync" type="info">重新同步</JButton>
    <JButton size="large" @click="refresh"   >刷新终端</JButton>
    <JButton size="large" @click="download"   >下载终端</JButton>
    <JButton size="large" @click="triggerFileInput"   >导入终端</JButton>
    <JButton size="large" @click="showSearch=true"   >搜索</JButton>
    <div style="height: 10px;"></div>
    <JGrid ref="jgridRef"
           size="small"
           :offset-width="2"
           :offset-bottom="120"
           show-pager
           :page-size="200"
           :search-model="model" listUrl="app/py"
           :columns="columns" />
  </div>
  <tiny-dialog-box v-model:visible="showTer" title="修改终端类型">
    <br />
    <br />
    <tiny-radio-group v-model="terType">
      <tiny-radio label="terType1">现饮</tiny-radio>
      <tiny-radio label="terType2">非现</tiny-radio>
      <tiny-radio label="empty">空白</tiny-radio>
    </tiny-radio-group>
    <br />
    <br />
    <br />
    <br />
    <tiny-button type="info" size="large" @click="saveAlt">保存修改</tiny-button>
    <br />
    <br />
    <br />
    <br />
  </tiny-dialog-box>
  <tiny-dialog-box v-model:visible="showSearch" title="搜索">
    <br />
    <br />
    <tiny-input placeholder="终端编码" v-model="model.buCode" clearable></tiny-input>
    <br />
    <br />
    <tiny-input placeholder="终端名称"  v-model="model.buName" clearable></tiny-input>
    <br />
    <br />
    <br />
    <tiny-button type="info" size="large" @click="doSearch">搜索</tiny-button>
    <br />
    <br />
    <br />
    <br />
  </tiny-dialog-box>
  <div style="height: 40px;width: 40px;">
    <input
        type="file"
        ref="fileInput"
        style="display: none"
        @change="handleFileChange"
    />

  </div>
</div>
</template>

<script setup>
import {ref, onMounted} from "vue";
import {TinyDialogBox} from "@opentiny/vue";
import tool from "@/utils/tool";
import {useRoute} from "vue-router";
const route = useRoute()
const showTer = ref(false)
const show = ref(false)
const showSearch = ref(false)
const columns = ref([
  {type: 'index', width: 50},
  {title: '终端类型', width: 90,field:'terType', align: 'center'},
  {title: '终端编码', width: 110,field:'buCode', align: 'center'},
  {title: '终端名称', field:'buName', width: 350,align: 'left', headerAlign: 'center'},
  {title: '联系人', field:'linkName', width: 110, align: 'left', headerAlign: 'center'},
  {title: '电话', field:'phone', width: 110, align: 'left', headerAlign: 'center'},
  {title: '地址', field:'address', width: 300,align: 'left', headerAlign: 'center'},
  {
    title: '操作',width: 90,fixed: 'right',
    btns: [
      {label: '修改',click: (row) => { alt(row) }},
    ]
  }
])
const terType = ref('empty')
const model = ref({
  which: 'getTerList',
  buCode: '',
  buName: '',
  terType: '',
})
const currentRow = ref({})
const list = ref([])
const jgridRef = ref(null)
const username = ref('')
const task = ref('')

const saveAlt = () => {
  if(currentRow.value.id) {
    tool.post('app/py', {which: 'saveTertype', formData:{id: currentRow.value.id, terType: terType.value}}, true).then(ret => {
      if(ret.stat) {
        currentRow.value.terType = ret.data
        showTer.value = false
      }
    })
  }
}
const  doSearch = () => {
  getList()
  showSearch.value = false
}
const startSync = () => {
  startPythonTask('syncTer')
}
const alt = (row) => {
  currentRow.value = row
  if(tool.xe.trim(row.terType) === '现饮') {
    terType.value = 'terType1'
  }
  else if(tool.xe.trim(row.terType) === '非现') {
    terType.value = 'terType2'
  }
  else if(tool.xe.trim(row.terType) === '') {
    terType.value = 'empty'
  }
  else {
    terType.value = 'empty'
  }
  showTer.value = true
}

const fileInput = ref(null);
const file = ref(null);
const triggerFileInput = () => {
  fileInput.value.click(); // 手动触发文件选择框
}
const handleFileChange = (event) => {
  file.value = event.target.files[0] // 获取选择的文件
  if (file.value) {
    uploadFile() // 自动上传文件
  }
}

// 上传文件
const uploadFile = async () => {
  if (!file.value) {
    return tool.err('请先选择文件')
  }
  try {
    const formData = new FormData()
    formData.append('file', file.value); // 将文件添加到 FormData
    tool.post('app/upload', formData,true, 'multipart/form-data').then(ret => {
      if(ret.stat && ret.data) {
        file.value = null
        fileInput.value.value = ''
        tool.post('app/py',{which: 'importTers',usernameAss:username.value, formData: {filename: ret.data}}).then(ret => {
          if(ret.stat) {
            getList()
          }
        })
      }
    })
  } catch (error) {
    file.value = null
    fileInput.value.value = ''
    tool.err('上传失败')
  }
}


const startPythonTask = (kind = '') => {
  if(typeof qt !== 'undefined') {
    new QWebChannel(qt.webChannelTransport, function (channel) {
      // 获取 Python 中注册的对象
      window.bridge = channel.objects.bridge;
      let send = {
        name: "startTask",
        kind
      }
      window.bridge.send_message(JSON.stringify(send), function (response) {

      })
    });
  }
}

onMounted(() => {
  username.value = route.query.username
  getList()
})
const refresh = () => {
  model.value.buName = ''
  model.value.buCode = ''
  getList()
}
const getList = () => {
  jgridRef.value.search()
}
const download = () => {
  jgridRef.value.exout()
}


const changeList = () => {
  if(list.value.length === 0) refresh()
}
defineExpose({changeList})
</script>
<style scoped lang="scss">
.sets {
  td{
    padding: 5px;
  }
}
.suc{
  color: green;
}
.err{
  color: red;
}
</style>